/**
 *  Copyright (c) 2010 The edtf project.  All rights reserved.
 *
 *  This file is part of edtf (Event Driven Testing Framework).
 *
 *  edtf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  edtf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with edtf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/edtf.
 */
package org.xepf.script;

import org.xepf.utils.Configuration;
import org.xepf.utils.ObjectCreator;
import org.xepf.xml.adapter.NodeSerializer;
import org.xepf.xml.adapter.XPath;
import org.xepf.eventboard.EventBoardManager;
import org.xepf.event.EventManager;
import java.util.List;
import org.xepf.xml.adapter.SimpleXPath;

/**
 *
 *
 */
public class ExecutionContext {

    private Configuration configuration;
    private String namespaceBinding = "";
    private String startWithScriptlet = "main";
    private String translator;
    private String script;
    private String sampleEvents;
    private boolean hasRealScripletThreading = false;
    private XPath xPath;
    private NodeSerializer s;
    private String defaultEventBoardName;
    private EventManager eventManager;
    private EventBoardManager boardManager;

    public ExecutionContext() throws Exception {
        xPath = (XPath) ObjectCreator.get("XPath");
        s = (NodeSerializer) ObjectCreator.get("NodeSerializer");
    }

    public void initialize(String xmlContext) throws Exception {
        xPath.initialize(xmlContext);
        
        namespaceBinding = s.serialize(xPath.query("/execution-context/nms-binding/@prefix"));
        startWithScriptlet = s.serialize(xPath.query("/execution-context/start-with/@scriplet"));
        if (startWithScriptlet.isEmpty()) {
            throw new Exception("/execution-context/@start-with must be present.");
        }
        
        List<String> eventBoards = s.serializeAsList(xPath.query("/execution-context/event-board"));
        if ( eventBoards.isEmpty() )
        {
            throw new Exception("At least one event-board must be declared in /execution-context/event-board.");
        } else {
            defaultEventBoardName = s.serialize(xPath.query("/execution-context/event-board/@name"));
        }
            
        
        boardManager.create(eventBoards);
        
        if ( xPath.query("/execution-context/real-scriplet-threading").isEmpty() == false )
        {
            hasRealScripletThreading = true;
        }
    }

    public String getNamespaceBinding() {
        return namespaceBinding;
    }

    public String getStartWithScriplet() {
        return startWithScriptlet;
    }

    public String getStartWithScripletDate() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getScripletWithName(String name) throws Exception {
        String getScriptXPath = "//scriplet[@name='" + name + "']";
        String xmlScriplet = s.serialize( SimpleXPath.query(script, getScriptXPath));
        return xmlScriplet;        
    }

    public String getTranslator() {
        return translator;
    }

    public void setTranslator(String translator) {
        this.translator = translator;
    }

    public void setScript(String script) {
        this.script = script;
    }

    public void setSampleEvents(String sampleEvents) {
        this.sampleEvents = sampleEvents;
    }

    public String getSampleEvents() {
        return sampleEvents;
    }
    
    public boolean hasRealScripletThreading() {
        return hasRealScripletThreading;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    String getScript() {
        return script;
    }

    public void setEventManager(EventManager eventManager) {
        this.eventManager = eventManager;
    }

    public void setEventBoardManager(EventBoardManager boardManager) {
        this.boardManager = boardManager;
    }

    public EventManager getEventManager() {
        return eventManager;
    }

    public String getDefaultBoardName() {
        return defaultEventBoardName;
    }
}
